package fi.solita.dojo.mazeserver.game.internal

import map._

final case class Player(val name: String,
		val coordinates: Coordinates,
		val heading: Directions.Direction, 
		color: (java.awt.Color, String), 
		points: Int, 
		nextAction: Option[GameAction]) {


	override def equals(other: Any): Boolean =
		other match {
		case that: Player => name == that.name
		case _ => false
	} // TODO using equals like this is not a good idea, rather rename to hasSameName(...)

	override def hashCode: Int = name.hashCode

	def freeToMove(): Boolean = !(gameLocation.hole)

	def gameLocation(): GameLocation = GameWorld.map()(coordinates)

	def validDirs(): List[Directions.Direction] = (Directions.elements filter (gameLocation.legalMove(_))).toList

	def validMove(d: Directions.Direction): Boolean = validDirs() contains d

	def move(d: Directions.Direction):  Player = {
		if (validMove(d))
			Player(name, gameLocation.neighbour(d).coordinates, heading, color, points, nextAction)
			else this
	}

	def turn(t: Turn.Turn):  Player = {
		Player(name, coordinates, Directions.turn(t, heading), color, points, nextAction)
	}

	def execute(a: GameAction): Player = {
		a match {
		case m: GameAction => {
			val loc = gameLocation.neighbour(Directions.East)
			Player(name, loc.coordinates, heading, color, points, nextAction)
		}
		}
	} 

	def addPoint(): Player = Player(name, coordinates, heading, color, points + 1, nextAction)

	override def toString(): String = {
			"(\"" + name + "\" ("  + 
					coordinates + ") " +
					heading + " " + points + ")"
	}

	def setAction(action: Option[GameAction]): Player = Player(name, coordinates, heading, color, points, action)
	
	def reset(): Player = Player(name, 
			GameWorld.initialLocation,
			Directions.randomDirection, 
			color, points, Some(DoNothing()))

}

